﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PerformanceAssessment.Models;

namespace PerformanceAssessment.Controllers
{ 
    public class SkillController : Controller
    {
        private PerformanceAssessmentDB db = new PerformanceAssessmentDB();

        //
        // GET: /Skill/

        public ViewResult Index()
        {
            return View(db.Skills.ToList());
        }

        //
        // GET: /Skill/Details/5

        public ViewResult Details(int id)
        {
            Skill skill = db.Skills.Find(id);
            return View(skill);
        }

        //
        // GET: /Skill/Create

        public ActionResult Create()
        {
            
            return View();
        }

        public ActionResult TeamSkillAssignment()
        {

            return View(db.Teams.ToList());
        }

        public ActionResult SkillsPerTeam(int teamId)
        {
            IQueryable<Skill> selectedSkills = from s in db.Skills
                                               join spt in db.SkillsPerTeam on s.ID equals spt.IdSkill
                                               where spt.IdTeam == teamId
                                               select s;
            var avSkills = db.Skills.Except(selectedSkills);
            ViewBag.AvailableSkills = avSkills.ToList();
            ViewBag.SelectedSkills = selectedSkills.ToList();
            return PartialView();
        }

        [HttpPost]
        public int SkillsPerTeam(int teamId, List<SkillsPerTeam> items)
        {
            if (teamId > 0)
            {
                db.Database.ExecuteSqlCommand("DELETE FROM SkillsPerTeam WHERE IdTeam = {0}", teamId);
                if (items != null && items.Count > 0)
                {
                    foreach (SkillsPerTeam spt in items)
                    {
                        db.SkillsPerTeam.Add(spt);
                    }
                }
                db.SaveChanges();
            }
            return 0;
        }

        //
        // POST: /Skill/Create

        [HttpPost]
        public ActionResult Create(Skill skill)
        {
            
            if (ModelState.IsValid)
            {
                db.Skills.Add(skill);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            return View(skill);
        }
        
        //
        // GET: /Skill/Edit/5
 
        public ActionResult Edit(int id)
        {
            Skill skill = db.Skills.Find(id);
            return View(skill);
        }

        //
        // POST: /Skill/Edit/5

        [HttpPost]
        public ActionResult Edit(Skill skill)
        {
            if (ModelState.IsValid)
            {
                db.Entry(skill).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(skill);
        }

        //
        // GET: /Skill/Delete/5
 
        public ActionResult Delete(int id)
        {
            Skill skill = db.Skills.Find(id);
            return View(skill);
        }

        //
        // POST: /Skill/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            Skill skill = db.Skills.Find(id);
            db.Skills.Remove(skill);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}